$(() => {
	loadData();
	initListener();
})

loadData = () => {
	startLoading();
	$("#list").html('');
	setTimeout(() => {
		queryLogisticsInfo(res => {
			hideLoading();
			if (res.code == 200) {
				let data = res.data;
				setList(data);
			} else {
				setLoadState('stateUlr', loginFail());
			}
		})
	}, 500)
	queryLogisticsInfo(res => {
		if (res.code == 200) {
			setList(res.data);
		} else {
			showToast(res.message);
		}
	})
}

setList = (mlist) => {
	$(`#list`).html('');
	if (ArrayEmpty(mlist)) {
		setLoadState('stateUlr', noData());
	} else {
		hideLoading();
		let itemHtml = `<div class="steps_list">`;

		mlist.forEach((e, i) => {

			let stateActive = ``;
			if (e.state == 1) {
				stateActive = `step_active`;
			}

			itemHtml += `<div class="step">
							<div class="step_status ${stateActive}">${e.stateName}</div>
							<div class="step_content">
								<div class="step_des">
									${e.courier}【${e.courierPhone}】
								</div>
								<span class="step_address">${e.address}</span>
								<span class="step_time">${e.time}</span>
							</div>
						</div>`;
		})
		$(`#list`).html(itemHtml);
	}
}

initListener = () => {
	$("#copy").on('click', () => {
		let text = $(`#bill_no`).val();
		copyText(text)
		//copyToClipboard(text);
	});
}

copyToClipboard = (text) => {
	var textarea = document.createElement('textarea');
	textarea.style.position = 'fixed';
	textarea.style.opacity = 0;
	textarea.value = text;
	document.body.appendChild(textarea);
	textarea.select();
	try {
		var successful = document.execCommand('copy');
		document.body.removeChild(textarea);
		var msg = successful ? '复制成功' : '失败';
		console.log('复制操作' + msg);
		showToast(msg)
	} catch (err) {
		console.log('Oops, unable to copy');
		showToast('暂不支持复制功能')
	}
}
